<?php
namespace Home\Controller;
use Think\Controller;
class LoginController extends Controller {

    //构造函数
    public function _initialize() {
        vendor('Ucenter.UcApi');//载入UcApi扩展
        //$this->autologin();
    }

    public function index() {
        //print_r($_COOKIE);
        $this->display();
    }

    //显示 测试
    public function show(){
        //print_r($_SESSION);exit;
        $this->assign('username',$_SESSION['username']);
        $this->display();
    }

    //登录
    public function login() {
        $this->display();
    }

    //检查登录
    public function checklogin() {
        if (!empty($_POST['username']) && !empty($_POST['password'])) {
            $login = UcApi::login($_POST['username'], $_POST['password']);
            if ($login === FALSE) {
                //$this->error(UcApi::getError());
                echo "<script>alert('登录失败，用户名或密码错误');history.back(-1);</script>";
            } else {
                $_SESSION['username'] = $login['username'];
                $_SESSION['user_id'] = $login['uid'];
                $_SESSION['email'] = $login['email'];
                echo $login['synlogin'];//输出同步登录代码 （这步很重要）
                echo "<script>alert('登录成功');window.location.href='/home/login/show';</script>";
				//$this->assign("jumpUrl", __APP__);
                //$this->success('登录成功');
            }
        } else {
            echo "<script>alert('登录失败，用户名或密码错误');history.back(-1);</script>";
            //$this->error('错误，用户名和密码不能为空');
        }
    }

    //注册
    public function register() {
        $this->display();
    }

    //检查注册
    public function check_register() {
        $reg = UcApi::reg($_POST['username'], $_POST['password'], $_POST['email']);
        if ($reg <= 0) {
            //$this->error(UcApi::getError());
            echo "<script>alert('注册失败');history.back(-1);</script>";
        } else {
            //$this->success('注册成功');
            echo "<script>alert('注册成功,请登录');window.location.href='/home/login/login';</script>";
        }
    }

    //退出登录
    public function logout() {
        session('[destroy]');
        echo UcApi::logout();	//输出同步登出的代码 （这步很重要）
        echo "<script>alert('退出成功');window.location.href='/home/login';</script>";
        //$this->assign('jumpUrl', __APP__.'/home/Login');
        //$this->success('退出成功');
    }

}

?>
